// CheckPrinterStatus.cpp : Defines the entry point for the DLL application.
//

#include "stdafx.h"

#include "com_ibm_saic_mes_pp_print_actions_CheckPrinterStatus.h"
JNIEXPORT jlong JNICALL Java_com_ibm_saic_mes_pp_print_actions_CheckPrinterStatus_checkStatus
(JNIEnv *env, jclass, jstring p){
	// char *ADevice, *ADriver, *APort;
	HANDLE hPrinter = NULL; 
	DWORD cByteNeeded,  returnStatus, cByteUsed; 
	JOB_INFO_2 *pJobStorage = NULL; 
	PRINTER_INFO_2 *pPrinterInfo = NULL; 
	unsigned char t = '0';
	if(!OpenPrinter((LPTSTR)env->GetStringUTFChars(p,&t),&hPrinter,NULL))
	{
		return 0x2;
	}
	/* Get the buffer size needed. */ 
	if (!GetPrinter(hPrinter, 2, NULL, 0, &cByteNeeded)) 
	{ 
		ClosePrinter(hPrinter);
		if (GetLastError() != ERROR_INSUFFICIENT_BUFFER) 
			return 0x2; 
	} 

	pPrinterInfo = (PRINTER_INFO_2 *)malloc(cByteNeeded); 
	if (!(pPrinterInfo)) 
	{
		/* Failure to allocate memory. */ 
		ClosePrinter(hPrinter);
		return 0x2; 
	}
	/* Get the printer information. */ 
	if (!GetPrinter(hPrinter, 2, (LPBYTE)pPrinterInfo,  cByteNeeded, &cByteUsed)) 
	{ 
		/* Failure to access the printer. */ 
		ClosePrinter(hPrinter);
		free(pPrinterInfo); 
		pPrinterInfo = NULL; 
		return 0x2; 
	} 
	returnStatus = pPrinterInfo->Status;
	ClosePrinter(hPrinter);
		free(pPrinterInfo); 
		pPrinterInfo = NULL; 
	return returnStatus;
}
